//package com.example.newbiechen.ireader.presenter;
//
//import android.util.Log;
//
//import com.example.newbiechen.ireader.model.bean.BookChapterBean;
//import com.example.newbiechen.ireader.model.bean.BookDetailBean;
//import com.example.newbiechen.ireader.model.bean.CollBookBean;
//import com.example.newbiechen.ireader.model.local.BookRepository;
//import com.example.newbiechen.ireader.model.remote.RemoteRepository;
//import com.example.newbiechen.ireader.presenter.contract.BookDetailContract;
//import com.example.newbiechen.ireader.service.boluoxs.BookChapterFinish;
////import com.example.newbiechen.ireader.service.boluoxs.BookChapterService;
//import com.example.newbiechen.ireader.service.boluoxs.BookService;
//import com.example.newbiechen.ireader.ui.base.RxPresenter;
//import com.example.newbiechen.ireader.utils.LogUtils;
//import com.example.newbiechen.ireader.utils.MD5Utils;
//
//import java.util.List;
//
//import io.reactivex.Single;
//import io.reactivex.SingleObserver;
//import io.reactivex.android.schedulers.AndroidSchedulers;
//import io.reactivex.disposables.Disposable;
//import io.reactivex.schedulers.Schedulers;
//
///**
// * Created by newbiechen on 17-5-4.
// */
//
//public class BookDetailPresenter extends RxPresenter<BookDetailContract.View>
//        implements BookDetailContract.Presenter{
//    private static final String TAG = "BookDetailPresenter";
//    private String bookId;
//
//    @Override
//    public void refreshBookDetail(String bookId) {
//        this.bookId = bookId;
//        refreshBook();
////        refreshComment();
////        refreshRecommend();
//
//    }
//
//    @Override
//    public void addToBookShelf(CollBookBean collBookBean)  {
////        if(collBookBean.getBookSrc() == 1){
////            this.addToBookShelf2(collBookBean);
////        }else {
//        Single<List<BookChapterBean>> single ;
//        if(collBookBean.getBookSrc() == 1){
//            single = BookService.getChaptersSingle(collBookBean.getChapterUrl(), collBookBean.get_id());
//        }else {
//            single = RemoteRepository.getInstance().getBookChapters(collBookBean.get_id());
//        }
//        Disposable disposable = single
//                    .subscribeOn(Schedulers.io())
//                    .doOnSubscribe(
//                            (d) -> mView.waitToBookShelf() //等待加载
//                    )
//                    .subscribeOn(AndroidSchedulers.mainThread())
//                    .observeOn(AndroidSchedulers.mainThread())
//                    .subscribe(
//                            beans -> {
//
//                                //设置 id
//                                for(BookChapterBean bean :beans){
//                                    bean.setId(MD5Utils.strToMd5By16(bean.getLink()));
//                                }
//
//                                //设置目录
//                                collBookBean.setBookChapters(beans);
//                                //存储收藏
//                                BookRepository.getInstance()
//                                        .saveCollBookWithAsync(collBookBean);
//
//                                mView.succeedToBookShelf();
//                            }
//                            ,
//                            e -> {
//                                mView.errorToBookShelf();
//                                LogUtils.e(e);
//                            }
//                    );
//            addDisposable(disposable);
////        }
//    }
//
//    public void addToBookShelf2(CollBookBean collBookBean){
//        mView.waitToBookShelf(); //等待加载
//        //获取所有章节
//        BookChapterService bookChapterService = new BookChapterService(collBookBean.getChapterUrl(), new BookChapterFinish() {
//            @Override
//            public void onFinish(List<BookChapterBean> chapterBeans) {
//                //设置目录
//                collBookBean.setBookChapters(chapterBeans);
//
//                collBookBean.setChaptersCount(chapterBeans.size());
////                collBookBean.setLastChapter(chapterBeans.get(chapterBeans.size()-1).getTitle());
//
//                //存储收藏
//                BookRepository.getInstance().saveCollBookWithAsync(collBookBean);
//
//                mView.succeedToBookShelf();
//            }
//        });
//        bookChapterService.download();
//    }
//
//    private void refreshBook(){
//        RemoteRepository
//                .getInstance()
//                .getBookDetail(bookId)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new SingleObserver<BookDetailBean>() {
//                    @Override
//                    public void onSubscribe(Disposable d) {
//                        addDisposable(d);
//                    }
//
//                    @Override
//                    public void onSuccess(BookDetailBean value){
//                        mView.finishRefresh(value);
//                        mView.complete();
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        mView.showError();
//                    }
//                });
//    }
//
//    private void refreshComment(){
//        Disposable disposable = RemoteRepository
//                .getInstance()
//                .getHotComments(bookId)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(
//                        (value) -> mView.finishHotComment(value)
//                );
//        addDisposable(disposable);
//    }
//
//    private void refreshRecommend(){
//        Disposable disposable = RemoteRepository
//                .getInstance()
//                .getRecommendBookList(bookId,3)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(
//                        (value) -> mView.finishRecommendBookList(value)
//                );
//        addDisposable(disposable);
//    }
//}
